package bb.lanxing.lib.devices.sprint;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import bb.lanxing.lib.devices.api.ConnectionListener;
import bb.lanxing.lib.devices.api.DeviceManager;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.core.ble.AbsBleDevice;
import bb.lanxing.lib.devices.core.dfu.AbsDfuAdapter;
import bb.lanxing.lib.devices.core.dfu.ICDMDfuDevice;
import bb.lanxing.util.Log;

public class SimpleBleDeviceDfuAdapter extends AbsDfuAdapter<AbsBleDevice> implements ConnectionListener {
    public SimpleBleDeviceDfuAdapter(Context context, AbsBleDevice absBleDevice) {
        super(context, absBleDevice);
        absBleDevice.registerConnectionListener(this);
    }

    @SuppressLint("MissingPermission")
    @Override
    public void enterDfuMode() {
        if (isDfuOn()) {
            notifyDufStateChanged();
            return;
        }
        ((AbsBleDevice) this.client).d("Try to enter dfu mode.");
        boolean z = false;
        if (this.client instanceof ICDMDfuDevice) {
            setDfuModeState(1);
            BluetoothGattCharacteristic cDMService = ((ICDMDfuDevice) this.client).getCDMService();
            cDMService.setValue(Commands.create(-33, null).toBytes());
            z = ((AbsBleDevice) this.client).getBluetoothGatt().writeCharacteristic(cDMService);
        }
        if (z) {
            ((AbsBleDevice) this.client).d("Send dfu cmd on success.");
            return;
        }
        ((AbsBleDevice) this.client).d("Send dfu cmd on failure.");
        setDfuModeState(4);
    }

    @Override
    public void onConnectionStateChanged(SmartDevice smartDevice, int i, int i2) {
        if (isDfuTurningOn() && i == DeviceManager.STATE_DISCONNECTED) {
            Log.d("Enter dfu mode on success.");
            setDfuModeState(2);
        }
    }
}
